package elements;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

public class Element implements Drawable {
	public final static int WALL1 =1;
	public final static int WALL2 =2;
	public final static int WALL3 =3;
	public final static int LASER =4;
	public final static int COIN =5;
	public final static int BLOCK =6;
	public final static int MIRROR =7;
	public final static int DMIRROR =8;
	public final static int TWOWAY1 =9;
	public final static int TWOWAY2 =10;
	public final static int  THREEWAY=11;
	public final static int EIGHTWAY =12;
	public final static int REFRACTION =13;
	public final static int STRAIGHT =14;
	public final static int CROSS =15;
	boolean isSelected;
	public int actType ;
	// 1 : can both move and rotate 2:can only move 3 :can only rotate 4: fixed
	int x;
	int y;
	int width = 40;
	int height =40;
	Image image;
	int rindex;
	int cindex;
	int color;
	int type ;
	int direction;
	/*      left up  right    
	 *        8   1   2
	 *        7   O   3
	 *        6   5   4 
	*/
	public Element(int rindex,int cindex, int color,int direction,int actType){
		this.setRindex(rindex);
		this.setCindex(cindex);
		this.color = color;
		this.direction = direction;
		this.actType = actType;
	}

	public boolean contains(int mousex,int mousey){
		return new Rectangle(x-width/2,y-height/2,width,height).contains(mousex,mousey);
	}
	
	// generate light if the element has its output rule. 
	//store like {{direction1,color1},
	//			  {direction2,color2},
	//			  {direction3,color3}}
	// if the subclass has its rule,pls override the  method
	public int[][] output(int inDirection, int inColor) {
		// TODO Auto-generated method stub
		
		return null;
	}

	
	public boolean canGo(int inDirection){
		return false;
	}
	@Override
	public void draw(Graphics2D g) {
		Graphics2D g2 = (Graphics2D)g;
		double radian =(direction+7)%8*45*Math.PI/180;// rotate 45 degree after one click
		g2.rotate(radian,x,y);
		g2.drawImage(image,x-width/2,y-height/2,width,height,null);
		g2.rotate(-radian,x,y);    //rotate back.
	}
	public void setPosition(int x,int y){
		this.x = x;
		this.y = y;
	}
	public void setDirection( int direction){
		if(direction==9)direction=1;
		else if(direction==0)direction=8;
		this.direction = direction;
	}
	public int getDirection(){
		return this.direction;
	}
	public int getRindex() {
		return rindex;
	}
	public void setRindex(int rindex) {
		this.rindex = rindex;
		this.y = TileMap.tileToPixel(rindex, 0)[0];
	}
	public int getCindex() {
		return cindex;
	}
	public void setCindex(int cindex) {
		this.cindex = cindex;
		this.x = TileMap.tileToPixel(0, cindex)[1];
	}
	public int getActType() {
		return actType;
	}

	public int getColor() {
		return color;
	}

	public int getType() {
		return type;
	}
}
